<?php

if (!class_exists("stfunctions_1_0")) {

    class stfunctions_1_0 {

        public $option = '';

        public function __construct($config) {
            $this->config = $config;
            $this->config ['pluginDir'] = WP_PLUGIN_DIR . '/' . $config ['plugin_name'] . '/';
            $this->option = get_option($this->config['plugin_name'].'_'.$this->config['option_key'].'_settings');
        }

        public function loadmodel($modelName) {
            $className = $modelName . "_model";
            $fileName = $this->config ['pluginDir'] . "models/{$modelName}.php";
            require_once $fileName;
            $obj = new $className($this->config);
            return $obj;
        }

        public static function getClientIp() {
            $ip;
            if (getenv("HTTP_CLIENT_IP"))
                $ip = getenv("HTTP_CLIENT_IP");
            else if (getenv("HTTP_X_FORWARDED_FOR"))
                $ip = getenv("HTTP_X_FORWARDED_FOR");
            else if (getenv("REMOTE_ADDR"))
                $ip = getenv("REMOTE_ADDR");
            else
                $ip = "UNKNOWN";
            return $ip;
        }

        public static function getBrowerType() {
            // Declare known browsers to look for
            $known = array('msie', 'firefox', 'safari', 'webkit', 'opera', 'netscape',
                'konqueror', 'gecko');

            // Clean up agent and build regex that matches phrases for known browsers
            // (e.g. "Firefox/2.0" or "MSIE 6.0" (This only matches the major and minor
            // version numbers.  E.g. "2.0.0.6" is parsed as simply "2.0"
            $agent = strtolower($agent ? $agent : $_SERVER['HTTP_USER_AGENT']);
            $pattern = '#(?<browser>' . join('|', $known) .
                    ')[/ ]+(?<version>[0-9]+(?:\.[0-9]+)?)#';

            // Find all phrases (or return empty array if none found)
            if (!preg_match_all($pattern, $agent, $matches))
                return array();

            // Since some UAs have more than one phrase (e.g Firefox has a Gecko phrase,
            // Opera 7,8 have a MSIE phrase), use the last one found (the right-most one
            // in the UA).  That's usually the most correct.
            $i = count($matches['browser']) - 1;
            return array('browser' => $matches['browser'][$i], 'version' => $matches['version'][$i]);
        }

        public static function sendMessage($message) {
            return "<script type='text/javascript'>"
            ."stcore.sendMessage('$message')"
            ."</script>"
            ;
        }

        public function pagination($start = NULL, $limit = NULL, $total = NULL
                , $cur_page = 1, $action = '') {

            if($total > $limit){                
                $start = $start? $start:0;
                $limit = $limit? $limit:$this->config['per_page'];
                $total = $total? $total:0;
                
                $total_page = ceil($total/$limit);
                $page_num = $this->config['page_num'];
                $cur_step = ceil($cur_page/$page_num);

                $max_page = ($cur_step * $page_num < $total_page)? $cur_step *$page_num:$total_page;

                $min_page = ($cur_step-1)*$page_num;
                $min_page = $min_page == 0? 1:$min_page;

                if( $cur_page == $max_page){
                    $min_page += 1;
                    $max_page = ($max_page + 1 < $total_page)? ($max_page + 1):$total_page;
                }

                $html = '<div id="st_pagination">';
                    $html .= '<ul>';
                    if($cur_page > 1){
                        $html .= "<li><a href='javascript:void(0)' onclick='stcore.paging(0, \"$action\")' >first</a></li>";
                        $html .= "<li><a href='javascript:void(0)' onclick='stcore.paging(".($cur_page - 1).", \"$action\")' >prev</a></li>";
                    }
                    for($i=$min_page; $i <= $max_page; $i++){
                        if($i == 0 || $i == $cur_page) $current = 'class="current"';
                        else $current = '';
                        $html .= "<li $current><a href='javascript:void(0)' onclick='stcore.paging($i, \"$action\")' >$i</a></li>";
                    }
                    if($cur_page < $total_page){
                        $html .= "<li><a href='javascript:void(0)' onclick='stcore.paging(".($cur_page + 1).", \"$action\")' >next</a></li>";
                        $html .= "<li><a href='javascript:void(0)' onclick='stcore.paging($total_page, \"$action\")' >last</a></li>";
                    }
                        
                    $html .= '</ul>';
                $html .= '</div>';
                return $html;
            }else{
                return;
            }
        }

    }

}
?>